let jv = require("libjv").default;
var ObjectId = require('mongodb').ObjectId;

const {logger} = require('../logger');

const jsdom = require("jsdom");
const {JSDOM} = jsdom;

// router.get('/', async (ctx, next) => {
//   await ctx.render('index', {
//     title: 'Hello Koa 2!'
//   })
// })

const mor = require("../libs/mongo/mongoHelper")


let work = async () => {
    var skip = 0;
    while (true) {
        var item = (await mor.find("jdCategory", {}, skip, 1))[0];
        if (!item) {
            break;
        }

        logger.info(item.name3)
        skip++;

        var c_url = item.url3;
        if (c_url.replace(/-/g, "").IsNumberFormat()) {
            c_url = "list.jd.com/list.html?cat=" + c_url.replace(/-/g, ",")
        }

        c_url = "https://" + c_url
        var c_res = await JSDOM.fromURL(c_url);
        var c_a = c_res.window.document.querySelector(".gl-item a")
        if (!c_a) {
            logger.error("找不到商品")
        } else {
            var c_href = c_a.href;
            if (!c_href) {
                logger.error("找不到href")
            } else {
                await jv.sleep(200);
                var p_res = await JSDOM.fromURL(c_href);
                var table = p_res.window.document.getElementsByClassName("Ptable")[0];
                if (!table) {
                    logger.error("找不到参数表")
                } else if (table.tagName == "DIV") {
                    var items = [];
                    Array.from(table.children).forEach(it => {
                        var p = {};
                        p.title = it.querySelector("h3").textContent;
                        p.parameters = [];

                        Array.from(it.querySelectorAll("dl.clearfix")).forEach(it => {
                            var kv = {};
                            kv.key = it.children[0].textContent;
                            kv.value = it.children[it.children.length - 1].textContent;
                            p.parameters.push(kv);
                        });

                        items.push(p);
                    });

                    await mor.update("jdCategory", {_id: item._id}, {table: items});
                } else if (table.tagName == "TABLE") {
                    var items = [];
                    var p = null;
                    Array.from(table.rows).forEach(it => {

                        var t = it.querySelector("th.tdTitle");
                        if (t) {
                            if (p) {
                                items.push(p);
                            }

                            p = {};
                            p.title = t.textContent
                            p.parameters = [];
                            return;
                        }

                        if (!p) {
                            logger.error("p is null！")
                            return;
                        }

                        if( it.children.length == 0){
                            return;
                        }

                        var kv = {};
                        kv.key = it.children[0].textContent;
                        kv.value = it.children[it.children.length - 1].textContent;
                        p.parameters.push(kv);

                    });

                    if(p) {
                        items.push(p);
                    }

                    await mor.update("jdCategory", {_id: item._id}, {table: items});
                }
            }
        }

        await jv.sleep(300);
    }
}


exports.default = work